/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import master_server.Element;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Administrateur
 */
public abstract class AbstractDAO {
   
    public List<? extends Element> getListe() {
        Session session = HibernateFactory.getSession();
        session.beginTransaction();
        Query query = session.createQuery(_queryList());
        List<? extends Element> lst = query.list();
        session.getTransaction().commit();
        session.close();
        return lst;
    }
    
    public Element select(int numElem) {
        Session session = HibernateFactory.getSession();
        Element elem = _select(session,numElem);
        session.close();
        return elem;
    }
    
    public void modif(Element elem) {
        Session session = HibernateFactory.getSession();
        Transaction tx = session.beginTransaction();
        session.saveOrUpdate(elem);
        tx.commit();
        session.close();
    }
    
    public void supprimer(Element elem) {
        Session session = HibernateFactory.getSession();
        session.delete(elem);
        session.flush();
        session.close();
    }
    
    protected abstract String _queryList();
    
    protected abstract Element _select(Session session, int num);
    
}
